package com.macro.mall.portal.controller.pms;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.common.api.util.StringUtils;
import com.macro.mall.pms.domain.Sku;
import com.macro.mall.pms.entity.ProductEntity;
import com.macro.mall.pms.entity.SkuEntity;
import com.macro.mall.pms.service.ProductService;
import com.macro.mall.pms.service.SkuService;
import com.macro.mall.pms.service.bo.PagedQuerySkuInput;
import com.macro.mall.portal.domain.FrontSku;
import com.macro.mall.portal.dto.pms.PagedQuerySkuReqDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * sku控制器
 *
 * @author xuyanjun
 * @date 2019-12-14
 */
@Api(tags = "SkuController", description = "sku")
@Slf4j(topic = "SkuController")
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProductService productService;

    /**
     * 分页查询sku
     *
     * @return 分页后的sku
     */
    @ApiOperation("分页查询sku")
    @PostMapping("pagedQuery")
    public CommonPage<Sku> pagedQuery(@RequestBody PagedQuerySkuReqDto reqDto) {
        PagedQuerySkuInput pagedQuerySkuInput = new PagedQuerySkuInput();
        pagedQuerySkuInput.setPageIndex(reqDto.getPageIndex()).setPageSize(reqDto.getPageSize());

        Page<SkuEntity> page = skuService.pagedQuery(pagedQuerySkuInput);

        List<Sku> skus = skuService.entityToDomain(page);

        return CommonPage.restPage(skus, page);
    }

    /**
     * 根据id获取sku
     *
     * @param id id
     * @return sku
     */
    @ApiOperation("根据id获取sku")
    @GetMapping("{id}")
    public CommonResult<FrontSku> getById(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException("要获取的skuId不能为空。");
        }

        SkuEntity skuEntity = skuService.getById(id);
        if (skuEntity == null) {
            throw new BusinessException(String.format("要获取的skuid:%s不存在。", id));
        }

        Sku sku = skuService.entityToDomain(skuEntity);

        return CommonResult.success(skuToFrontSku(sku));
    }

    private FrontSku skuToFrontSku(Sku sku) {
        FrontSku frontSku = new FrontSku();

        ProductEntity productEntity = productService.getById(sku.getProductId());
        BeanUtil.copyProperties(sku, frontSku);

        frontSku.setAttr(StringUtils.format("{} {} {}", sku.getSp1()==null?"":sku.getSp1(),
                sku.getSp2()==null?"":sku.getSp2(),sku.getSp3()==null?"":sku.getSp3()));
        frontSku.setProductName(productEntity.getName());
        frontSku.setProductTitle(productEntity.getSubTitle());
        frontSku.setCommission(productEntity.getCommission());

        return frontSku;
    }

    /**
     * 根据批量id获取sku
     *
     * @param ids ids
     * @return sku
     */
    @ApiOperation("根据批量id获取sku")
    @PostMapping("listByBatchIds")
    public CommonResult<List<Sku>> listByBatchIds(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("要获取的skuId集合不能为空。");
        }

        List<SkuEntity> skuEntities = skuService.listByBatchIds(ids);

        List<Sku> skus = skuService.entityToDomain(skuEntities);
        return CommonResult.success(skus);
    }

}

